//
//  UserAccount.swift
//  ydy_weibo
//
//  Created by 杨德勇 on 16/7/16.
//  Copyright © 2016年 杨德勇. All rights reserved.
//

import UIKit

class UserAccount: NSObject,NSCoding{
    ////用于调用access_token，接口获取授权后的access token。
    var access_token: String?
    //access_token的生命周期，单位是秒数
    var expires_in: NSTimeInterval = 0 {
        didSet{
            expires_date = NSDate(timeIntervalSinceNow: expires_in)
        }
        
    }
    
    var expires_date : NSDate?
    //当前授权用户的UID
    var uid: String?
    //用户头像地址 String
    var avatar_large: String?
    //用户名
    var name: String?
    
    
    init(dict:[String:AnyObject]){
        super.init()
        //注意：因为返回的信息有很多多余的字段，如果不重写undefinedkey程序会崩溃
        setValuesForKeysWithDictionary(dict)
        
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    
    //MARK:-
    //MARK:对象描述信息

    //重写这个方法的时候返回对象的描述信息（如果不写返回内存地址）
    override var description: String{
        let keys = ["access_token","expires_in","uid","avatar_large","name","expires_date"]
        return dictionaryWithValuesForKeys(keys).description
    }
    
    //保存方法
    func saveAccount() {
        let path = (NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last! as String) + "/account.plist"
        NSKeyedArchiver.archiveRootObject(self, toFile: path)
        print(path)
        
    }
    
    //MARK:-
    //MARK:读取文件
    class func loadAccount() -> UserAccount? {
        let path = (NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last! as String) + "/account.plist"
        if let account = NSKeyedUnarchiver.unarchiveObjectWithFile(path) as? UserAccount{
            //这里需要判断一下登陆是否过期了
            
            //比较时间:compare前的对象作为第一个，compare后面的对象作为第2个，比如说第一个时间比第2个时间大，说明还没过期，那么它两是降序排列（Descending）
            if account.expires_date?.compare(NSDate()) == NSComparisonResult.OrderedDescending  {
                return account
            }
            
        }
        return nil
    }

    
    //MARK:-
    //MARK:归档方法（nscoding）
    

    

    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(self.access_token, forKey: "access_token")
        aCoder.encodeObject(self.avatar_large, forKey: "avatar_large")
        aCoder.encodeDouble(self.expires_in, forKey: "expires_in")
        aCoder.encodeObject(self.name, forKey: "name")
        aCoder.encodeObject(self.uid, forKey: "uid")
        aCoder.encodeObject(self.expires_date, forKey: "expires_date")
        
    }
   //这个init方法不能写在扩展类里面，而且不需要return了。
    required init?(coder aDecoder: NSCoder) {
        self.access_token = aDecoder.decodeObjectForKey("access_token") as? String
        self.avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
        self.expires_in  = aDecoder.decodeDoubleForKey("expires_in")
        self.name = aDecoder.decodeObjectForKey("name") as? String
        self.uid = aDecoder.decodeObjectForKey("uid") as? String
        self.expires_date = aDecoder.decodeObjectForKey("expires_date") as? NSDate
        
    }
    
}
